import React from 'react';
import ReactDOM from "react-dom";
import LifeChild from "./LifeChild";

class LifeParent extends React.Component {

    constructor() {
        super();
        this.state = {
            hasError: false,
            count: 0,
        };
        console.log("LifeParent constructor ...");
    }

    static getDerivedStateFromProps(props, state) {
        console.log("LifeParent getDerivedStateFromProps ...", props, state);
        return state;
    }

    onClickAddCount = () => {
        this.setState({
            count: this.state.count + 1,
        });
    };

    render() {
        console.log("LifeParent render ...");
        return (
            <>
                <div>{this.state.count}</div>
                {console.log(this.state.hasError)}
                {
                    
                    !this.state.hasError ? (
                        <LifeChild onClick={this.onClickAddCount} />
                    ): <>678</>
                }
            </>
        );
    }

    componentDidMount() {
        console.log("LifeParent componentDidMount ...");
        // const timer = setTimeout(() => {
        //     this.setState({
        //         count: this.state.count + 1,
        //     });
            // ReactDOM.unmountComponentAtNode(document.querySelector("#root"));
        //     clearTimeout(timer);
        // }, 3000);
    }

    shouldComponentUpdate(nextProps, nextState) {
        console.log("LifeParent shouldComponentUpdate ...", nextProps, nextState);
        return true;
    }

    getSnapshotBeforeUpdate(prevProps, prevState) {
        console.log("LifeParent getSnapshotBeforeUpdate ...", prevProps, prevState);
        return null;
    }

    componentDidUpdate(prevProps, prevState) {
        console.log("LifeParent componentDidUpdate ...", prevProps, prevState);
    }

    componentWillUnmount() {
        console.log("LifeParent componentWillUnmount ...");
    }

    static getDerivedStateFromError(error) {
        console.log("LifeParent getDerivedStateFromError ...", error);
        alert("ada");
        // fetch("http://localhost:3006/api/testError")
        // .then(res => res.json())
        // .then(res => {
        //     console.log(res);
        // });
        return { hasError: true };
    }

    componentDidCatch(error, info) {
        console.log("LifeParent componentDidCatch ...", error, info);
    }
}

export default LifeParent;